<?php
/**
 * Photoassociation
 *
 * Description de la classe
 *
 * @author Valentin
 */
class PhotoProduct extends Object {
    protected $id;
    protected $produit_id;
    protected $nom;
    protected $lien;

    protected $product;

    function __construct($id="", $params=array()) {
        if($id!='') {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }

    function __toString() {
        return $this->nom;
    }

    public function load() {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM photos WHERE id = '".$this->id."'");
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }

    public function save() {
        $db = Database::getInstance();
        if (!isset($this->id)) {
            $sql="INSERT INTO photos (produit_id,nom,lien)
                            VALUES  ('".$this->produit_id."',
                                    '".$this->nom."',
                                    '".$this->lien."'
                                     )
                                  ";
            $result = $db->query($sql);
            $this->id=$db->getInsertedId();
        }
        else{
            $sql="UPDATE photos SET  produit_id='".$this->produit_id."',
                                    nom='".$this->nom."',
                                    lien='".$this->lien."'

                                WHERE  id='".$this->id."'  ";
            $result = $db->query($sql);
        }
    }
    
    public function getMaxi() {
        if(!isset($this->produit_id)|| !isset($this->nom)) $this->load();
		$product=$this->getProduct();
		$vendor=$product->getVendor();
         return IMG_PATH.'vendors/'.$vendor['id'].'/maxi_'.$this->nom;
    }
    public function getMini() {
        if(!isset($this->produit_id)|| !isset($this->nom)) $this->load();
		$product=$this->getProduct();
		$vendor=$product->getVendor();
         return IMG_PATH.'vendors/'.$vendor['id'].'/mini_'.$this->nom;
    }
    public function getListing() {
        if(!isset($this->produit_id)|| !isset($this->nom)) $this->load();
		$product=$this->getProduct();
		$vendor=$product->getVendor();
				
        return IMG_PATH.'vendors/'.$vendor['id'].'/listing_'.$this->nom;
    }

    public function delete(){
        $db= Database::getInstance();
        $sql="DELETE FROM photos WHERE id = '".$this->id."' ";
        $db->query($sql);
    }
    public function getBig() {
        if(!isset($this->produit_id)|| !isset($this->nom)) $this->load();
        return IMG_PATH.'vendor/'.$this->lien.$this->nom;
    }

    public function getProduct() {
        if(isset($this->product)) return $this->product;
        if(!isset($this->produit_id)) $this->load();
        $this->product = new Product($this->produit_id);
        return $this->product;
    }
}
?>
